package com.lyf.scm.admin.domain.remote.allocationservice.facade;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.remote.allocationservice.SalesPlanDirectlyRemote;
import com.lyf.scm.common.enums.ResponseMsg;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * description:
 * 直营预测计划
 *
 * @author: haodong
 * @data: 2020/8/4 9:49
 */
@Service
@Slf4j
public class SalesPlanDirectlyFacade {

    @Resource
    private SalesPlanDirectlyRemote salesPlanDirectlyRemote;

    /**
     * 按条件查询直营预测计划
     *
     * @param salesPlanDirectly
     * @return
     */
    public PageInfo<SalesPlanDirectlyReturnDTO> listPageBySelective(SalesPlanDirectlyDTO salesPlanDirectly) {
        log.info("直营预测计划查询入参:SalesPlanDirectlyDTO:{}", JSON.toJSONString(salesPlanDirectly));
        Response<PageInfo<SalesPlanDirectlyReturnDTO>> response = salesPlanDirectlyRemote.listPageBySelective(salesPlanDirectly);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    /**
     * 查询全部直营预测计划
     *
     * @param salesPlanDirectly
     * @return
     */
    public List<SalesPlanDirectlyReturnDTO> listAllByCycles(SalesPlanDirectlyDTO salesPlanDirectly) {
        log.info("查询全部直营预测计划入参:SalesPlanDirectlyDTO:{}", JSON.toJSONString(salesPlanDirectly));
        Response<List<SalesPlanDirectlyReturnDTO>> response = salesPlanDirectlyRemote.listAllByCycles(salesPlanDirectly);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    /**
     * 更新确认量
     *
     * @param paramList
     * @return
     */
    public Boolean updateConfirm(List<SalesPlanDirectlyDTO> paramList, Long unitType, Integer level) {
        log.info("更新确认量入参:SalesPlanDirectlyDTO:{}", JSON.toJSONString(paramList));
        Response<Boolean> response = salesPlanDirectlyRemote.updateConfirm(paramList, unitType, level);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    /**
     * 导出Excel
     *
     * @param directly
     * @param type
     * @return
     */
    public List<SalesPlanDirectlyWriteDTO> exportExcel(SalesPlanDirectlyDTO directly, Integer type) {
        log.info("直营预测计划导出Excel入参:SalesPlanDirectlyDTO:{}", JSON.toJSONString(directly));
        Response<List<SalesPlanDirectlyWriteDTO>> response = salesPlanDirectlyRemote.exportExcel(directly, type);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public Boolean importVerification(List<SalesPlanDirectlyDTO> directlyWrite) {
        Response<Boolean> response = salesPlanDirectlyRemote.importVerification(directlyWrite);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public void delete(String opSign) {
        log.info("删除直营预测计划对应标识:{}", JSON.toJSONString(opSign));
        salesPlanDirectlyRemote.delete(opSign);
    }

    public PageInfo<EsReturnDTO> queryByGroup(EsAggregationDTO esQuery) {
        Response<PageInfo<EsReturnDTO>> response = salesPlanDirectlyRemote.queryByGroup(esQuery);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

}
